package kr.or.kead.daegu.global;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.or.kead.daegu.global.InputUI.InputBaseUI;
import kr.or.kead.daegu.global.InputUI.InputSaleMgnUI;
import kr.or.kead.daegu.global.OutputUI.OutputUI;

public class ProjGlobal2013 implements ActionListener {

	private JFrame frame;
	private JMenuItem mItemProductMgn;
	private JMenuItem mItemSaleEmpMgn;
	private JMenuItem mItemBusinessMgn;
	private JMenuItem mItemSaleMgn;
	private JMenuItem mItemHstByPdt;
	private JMenuItem mItemEmp;
	private JMenuItem mItemProduct;
	private JMenuItem mItemHstByBsn;
	private JMenuItem mItemHstByEmp;
	private JMenuItem mItemBsn;
	private JPanel mainPanel;
	
	private OutputUI outEmpUI;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProjGlobal2013 window = new ProjGlobal2013();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ProjGlobal2013() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("거래처 영업 관리 프로그램");
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu menuBasicMgn = new JMenu("기초자료관리");
		menuBar.add(menuBasicMgn);
		
		mItemSaleEmpMgn = new JMenuItem("영업사원관리");
		mItemSaleEmpMgn.addActionListener(this);
		menuBasicMgn.add(mItemSaleEmpMgn);
		
		mItemProductMgn = new JMenuItem("제품관리");
		mItemProductMgn.addActionListener(this);
		menuBasicMgn.add(mItemProductMgn);
		
		mItemBusinessMgn = new JMenuItem("거래처관리");
		mItemBusinessMgn.addActionListener(this);
		menuBasicMgn.add(mItemBusinessMgn);
		
		JMenu menuSaleMgn = new JMenu("판매관리");
		menuBar.add(menuSaleMgn);
		
		mItemSaleMgn = new JMenuItem("판매관리");
		mItemSaleMgn.addActionListener(this);
		menuSaleMgn.add(mItemSaleMgn);
		
		JMenu menuQuery = new JMenu("조회업무");
		menuBar.add(menuQuery);
		
		mItemEmp = new JMenuItem("영업사원현황");
		mItemEmp.addActionListener(this);
		menuQuery.add(mItemEmp);
		
		mItemProduct = new JMenuItem("제품현황");
		mItemProduct.addActionListener(this);
		menuQuery.add(mItemProduct);
		
		mItemBsn = new JMenuItem("거래처현황");
		mItemBsn.addActionListener(this);
		menuQuery.add(mItemBsn);
		
		mItemHstByBsn = new JMenuItem("거래처별 거래 현황");
		mItemHstByBsn.addActionListener(this);
		menuQuery.add(mItemHstByBsn);
		
		mItemHstByPdt = new JMenuItem("제품별 거래 현황");
		mItemHstByPdt.addActionListener(this);
		menuQuery.add(mItemHstByPdt);
		
		mItemHstByEmp = new JMenuItem("영업사원별 거래 현황");
		mItemHstByEmp.addActionListener(this);
		menuQuery.add(mItemHstByEmp);
		
		mainPanel = new JPanel();
		frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == mItemBsn) {
			mItemBsnActionPerformed(e);
		}
		if (e.getSource() == mItemHstByEmp) {
			mItemHstByEmpActionPerformed(e);
		}
		if (e.getSource() == mItemHstByBsn) {
			mItemHstByBsnActionPerformed(e);
		}
		if (e.getSource() == mItemProduct) {
			mItemProductActionPerformed(e);
		}
		if (e.getSource() == mItemEmp) {
			mItemEmpActionPerformed(e);
		}
		if (e.getSource() == mItemHstByPdt) {
			mItemHstByPdtActionPerformed(e);
		}
		if (e.getSource() == mItemSaleMgn) {
			mItemSaleMgnActionPerformed(e);
		}
		if (e.getSource() == mItemBusinessMgn) {
			mItemBusinessMgnActionPerformed(e);
		}
		if (e.getSource() == mItemSaleEmpMgn) {
			mItemSaleEmpMgnActionPerformed(e);
		}
		if (e.getSource() == mItemProductMgn) {
			mItemProductMgnActionPerformed(e);
		}
	}
	
	/**
	 * 변경된 UI 새로고침
	 * @param 갱신하고자하는 UI JPanel
	 */
	private void reloadUI(JPanel p){
		Container contentPane = frame.getContentPane();		
		contentPane.removeAll();
		contentPane.add(p);
		frame.setSize(p.getPreferredSize());
		contentPane.validate();
	}
	
	protected  void mItemProductMgnActionPerformed(ActionEvent e) {
		mainPanel = new InputBaseUI(1,-1);
		reloadUI(mainPanel);
	}
	
	protected  void mItemSaleEmpMgnActionPerformed(ActionEvent e) {
		mainPanel = new InputBaseUI(0,0);
		reloadUI(mainPanel);
	}
	
	protected  void mItemBusinessMgnActionPerformed(ActionEvent e) {
		mainPanel = new InputBaseUI(2,1);
		reloadUI(mainPanel);
	}
	protected  void mItemSaleMgnActionPerformed(ActionEvent e) {
		mainPanel = new InputSaleMgnUI();
		reloadUI(mainPanel);
	}

	protected  void mItemEmpActionPerformed(ActionEvent e) {
		outEmpUI = new OutputUI(0);
	}
	
	protected  void mItemProductActionPerformed(ActionEvent e) {
		outEmpUI = new OutputUI(1);
	}
	
	protected  void mItemBsnActionPerformed(ActionEvent e) {
		outEmpUI = new OutputUI(2);
	}
	
	protected  void mItemHstByBsnActionPerformed(ActionEvent e) {
		JOptionPane.showMessageDialog(null, e.getActionCommand());
	}
	
	protected  void mItemHstByEmpActionPerformed(ActionEvent e) {
		JOptionPane.showMessageDialog(null, e.getActionCommand());
	}
	
	protected  void mItemHstByPdtActionPerformed(ActionEvent e) {
		JOptionPane.showMessageDialog(null, e.getActionCommand());
	}
}
